const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const htmlBenPlugin = require('./ben/plugin/html')
const {
	getfiles
} = require("./ben/benTool")
const components = require("./ben/benComponentsLoad")

const mpaConfig = () => {
	const entry = {};
	const htmlPlugin = [];

	getfiles(path.join(__dirname, '/src/pages/')).map(dir => {
		const pageName = dir.name;
		entry[pageName] = path.join(__dirname, `/src/pages/${pageName}/${pageName}.js`);
		htmlPlugin.push(
			new HtmlWebpackPlugin({
				inject: true,
				caseSensitive: true,
				filename: pageName + '.html', // 输出的html名称，默认为 index.html
				template: path.join(__dirname, `/src/pages/${pageName}/${pageName}.html`),
				chunks: [pageName], // 该 vendor 文件是使用 splitChunks打包出的公共 node_modules js文件，如果没用则不用加
				minify: {
					caseSensitive: true,
					keepClosingSlash: true
				}
			}),
		)
	});

	return {
		entry,
		htmlPlugin,
	}
};

const {
	entry,
	htmlPlugin
} = mpaConfig()

const config = {
	entry,
	output: {
		path: path.resolve(__dirname, 'dist'),
		filename: '[name].js'
	},

	module: {
		rules: [{
			test: /\.vue$/,
			use: [
				path.resolve(__dirname, './loader/vue')
			]
		}]
	},
	plugins: [new htmlBenPlugin(components), ...htmlPlugin],
	// mode: 'production'
};
module.exports = config;